package cn.gov.pbc.tims
.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.ObjectMapper;

import cn.gov.pbc.tims.entity.Dept;
import cn.gov.pbc.tims.entity.Task;
import cn.gov.pbc.tims.entity.User;
import cn.gov.pbc.tims.service.CommonService;
import cn.gov.pbc.tims.util.Operator;
import cn.gov.pbc.tims.util.PageData;
import cn.gov.pbc.tims.util.Result;;

@Controller
@RequestMapping("/")
public class TaskController {
	private static final Logger LOGGER = LogManager.getLogger(SearchController.class);
	private CommonService commonService;
	@Autowired
	private ObjectMapper objectMapper;

	@Autowired
	private PageData grid_data;

	@Autowired
	private Operator operator;

	@Autowired
	private FileController fileController;

	@Autowired
	public void setCommonService(CommonService commonService) {
		this.commonService = commonService;
	}

	@Autowired
	private SessionFactory sessionFactory;

	private Session getCurrentSession() {
		return this.sessionFactory.getCurrentSession();
	}

	 

	@RequestMapping(value = "Task/save")
	public @ResponseBody Result saveTask(Task entity,String buchong) {
		
		User user=operator.getUser();
		String deptIds=entity.getDeptIds();
		if(null!=deptIds)
		{
			String deptId1=deptIds.split(",")[0];
			Dept dept=commonService.get(Dept.class, Integer.valueOf(deptId1));
			if(!dept.getLevel().equals("1"))
				dept=commonService.get(Dept.class,dept.getUpDeptId());
			entity.setL1DeptId(dept.getId());
		} 
		
		entity.setCreator(user.getName());
		entity.setCreatorCode(user.getCode());
		entity.setJgId(user.getJgId());
		entity.setJgName(user.getJgName());
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String str = format.format(Calendar.getInstance().getTime());
		if(null!=entity.getId())
		{	
			/*String hql="select content from Task where id=?";
			String content=(String)commonService.findHql(hql, new Object[]{entity.getId()}).get(0);
		    if(!content.equals(entity.getContent()))
		    entity.setContent(entity.getContent()+"\r\n"+ "          edited by "+user.getName()+" at "+str);*/
		    if(null!=buchong)
			{String content=entity.getContent()+"\r\n"+buchong+"\r\n"+ "          edited by "+user.getName()+" at "+str;
			entity.setContent(content);
			}
		}
		else
			entity.setContent(entity.getContent()+"\r\n"+ "          submited by "+user.getName()+" at "+str);
			
		commonService.saveOrUpdate(entity);
	 
	 	Result result = new Result();
		result.setMsg("保存成功！");
		result.setCode(0);

		return result;
	}

	
	@RequestMapping(value = "Task/del")
	public @ResponseBody Result delTask(Task entity) {
		commonService.delete(entity);
	 
	 	Result result = new Result();
		result.setMsg("删除成功！");
		result.setCode(0);

		return result;
	}
	@RequestMapping(value = "Task/getcount")
	public @ResponseBody Long getcount() {
		User user=operator.getUser();
		String hql="from Task where  status='0' and deptId=?";
		List list=new ArrayList();
		list.add(user.getDeptId());
		Long  count=commonService.getCount(hql, list);
	 
		return count;
	} 

}
